package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;

import com.example.ui.contact.bean.BaseItem;
import com.example.ui.contact.bean.MeItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MeMsgDB {
	public static final String MSG_DBNAME = "Msg.db";
	private static final String MeMsg_TABLE_NAME = "meMsg";
	private SQLiteDatabase db;
	@SuppressWarnings("static-access")
	public MeMsgDB(Context context){
		db = context.openOrCreateDatabase(MSG_DBNAME,Context.MODE_APPEND,null);
		db.execSQL("CREATE table IF NOT EXISTS "
				+ MeMsg_TABLE_NAME
				+ " (MeMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,userId TEXT, userId2 TEXT," +
				"typeId TEXT,nickname TEXT,communityId TEXT,txt TEXT,dateline TEXT ,location TEXT,name TEXT,headPic TEXT)");
		//test
		//		db.execSQL("CREATE table IF NOT EXISTS "
		//				+ MeMsg_TABLE_NAME
		//				+ " (MeMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,userId TEXT)");	
		
	}

	public void saveMeMsg(MeItem item){
//		if (isExist(item.getUserId())) {
//			ContentValues cv = new ContentValues();
//			cv.put("userId", item.getUserId());
//
//
//			db.update(MeMsg_TABLE_NAME, cv, "userId=?",
//					new String[] { item.getUserId() });
//		} else {
//		}	

		
		db.execSQL(
				"insert into "
						+ MeMsg_TABLE_NAME
						+ " (userId,userId2,typeId,nickname,communityId,txt,dateLine,location,name,headPic) "
						+"values(?,?,?,?,?,?,?,?,?,?)",
				new Object[] { item.getUserId(), item.getUserId2(),item.getType(),item.getNick(), item.getCommunityId(),
							   item.getTxt(), item.getDateLine(),item.getLocation(),item.getName(),
							   item.getHeadPic()});
		///////test
		//		db.execSQL(
		//				"insert into "
		//						+ MeMsg_TABLE_NAME
		//						+ "(userId)"
		//						+"values(?)",
		//				new Object[] { item.getUserId()});		
	}

	public LinkedList<MeItem> getMeMsgList() {
		LinkedList<MeItem> list = new LinkedList<MeItem>();
		Cursor c = db.rawQuery("SELECT * from " + MeMsg_TABLE_NAME, null);
		while (c.moveToNext()) {
			String userId = c.getString(c.getColumnIndex("userId"));
			String userId2 = c.getString(c.getColumnIndex("userId2"));
			String typeId = c.getString(c.getColumnIndex("typeId"));
			String nickname = c.getString(c.getColumnIndex("nickname"));
			String communityId = c.getString(c.getColumnIndex("communityId"));
			String txt = c.getString(c.getColumnIndex("txt"));
			String dateLine = c.getString(c.getColumnIndex("dateline"));
			String location = c.getString(c.getColumnIndex("location"));
			String name = c.getString(c.getColumnIndex("name"));
			String headPic = c.getString(c.getColumnIndex("headPic"));

			MeItem item = new MeItem();
			item.setUserId(userId);
			item.setUserId2(userId2);
			item.setType(typeId);
			item.setNick(nickname);
			item.setCommunityId(communityId);
			item.setTxt(txt);
			item.setDateLine(dateLine);
			item.setLocation(location);					
			item.setName(name);
			item.setHeadPic(headPic);			
			list.add(item);
		}
		//Collections.sort(list);// 按时间降序
		return list;
	}
	
	public void close() {
		if (db != null)
			db.close();
	}
	
	
	private boolean isExist(String userId) {
		Cursor c = db.rawQuery("SELECT * FROM " + MSG_DBNAME
				+ " WHERE userId = ?", new String[] { userId });
		return c.moveToFirst();
	}	
	
}
